var ValidateLayout = function () {
	return {
		init:function() {
			var layValidator = $('.form-layout').validate({
				errorElement: 'span', //default input error message container
				errorClass: 'help-block', // default input error message class
				focusInvalid: false, // do not focus the last invalid input
				rules: {
					oldLoginPwd: {
						required: true,
						remote:{
							type: "post",
							url: layUrl,
							data: {
								oldLoginPwd: function() {
									return $("#oldLoginPwd").val();
								},
								ajax:"checkPwd"
							}, 
							dataType: "json"
						}
					},
					newLoginPwd: {
						required: true,
						rangelength:[5,16] 
					},
					reNewLoginPwd: {
						required: true,
						rangelength:[5,16] ,
						equalTo:"#newLoginPwd"
					},
					
				},

				messages: {
					oldLoginPwd: {
						required: "原始密码不能为空。",
						regex:"用户名只能用数字字母下划线。",
						remote:"原始密码不正确！"
					},
					newLoginPwd: {
						required: "新密码不能为空。",
						rangelength: "密码最少5个字符最多16个字符。"
					},
					reNewLoginPwd: {
						required: "密码不能为空。",
						rangelength: "密码最少5个字符最多16个字符。",
						equalTo:"请输入相同的新密码！"
					}	
				},

				invalidHandler: function (event, validator) { //display error alert on form submit
					$('.alert-danger', $('.form-layout')).show();
				},

				highlight: function (element) { // hightlight error inputs
					$(element)
						.closest('.form-group').addClass('has-error'); // set error class to the control group
				},

				success: function (label) {
					label.closest('.form-group').removeClass('has-error');
					label.remove();
				},

				errorPlacement: function (error, element) {
					error.insertAfter(element.closest('.input-icon'));
				},

				submitHandler: function (form) {					
					$.post(layUrl,$(".form-layout").serialize(),function(data) {
						if(data.status=='1'){
							layer.msg(data.info, 
								{icon: 1,time: 2000},
								function(){
									$('#basic').modal('hide');
									$('#basic :input').val('');
									$("#basic input[name='ajax']").val('savePass');
								}
							);   	
						}else{
							layer.msg(data.info, 
								{icon: 2,time: 2000}
							);   
							return;
						}
					});           
					
				}
			});
			
			$('.form-layout input').keypress(function (e) {
				if (e.which == 13) {
					if ($('.form-layout').validate().form()) {
						$('.form-layout').submit();
					}
					return false;
				}
			});
			
			//取消按钮
			$('button.noLay').live('click', function (e) {
                e.preventDefault();
				$('#basic').modal('hide');//关闭模态框
				layValidator.resetForm();//重置验证表单
				$('#basic .form-group').removeClass('has-error');//去除遗留样式	
				$('#basic :input').val('');
				$("#basic input[name='ajax']").val('savePass');
            });
			

			
		}
    };
}();